package android.slc.extras.component.webview

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.slc.appbase.ui.activity.base.AppMvvmBaseToolBarActivity
import android.slc.appbase.R
import android.slc.appbase.config.ConstantsBase
import android.slc.appbase.databinding.AppActivityWebViewBinding
import android.slc.appbase.utils.AppIntentUtils

/**
 * @author slc
 * @date 2024/8/7
 **/
class AppWebViewActivity : AppMvvmBaseToolBarActivity<AppActivityWebViewBinding, AppWebViewVm>() {

    companion object {
        fun startAppWebView(context: Context, title: String, url: String) {
            context.startActivity(
                AppIntentUtils.getIntentByBundle(AppWebViewActivity::class.java,
                    Bundle().apply {
                        this.putString(ConstantsBase.Key.KEY_TITLE_LABEL, title)
                        this.putString(AppIntentUtils.KEY_INTENT_DATA, url)
                    }
                ))
        }
    }

    override fun setContentView(): Any {
        return R.layout.app_activity_web_view
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onBindView(savedInstanceState: Bundle?) {
        super.onBindView(savedInstanceState)
        mSlcToolBarDelegate.setTitle(getTitleByIntent(""))
        dataBinding.webView.settings.javaScriptEnabled = true
    }

    override fun onRegisterLiveEvent() {
        super.onRegisterLiveEvent()
        viewModel.urlLd.observe(this) { url ->
            dataBinding.webView.loadUrl(url)
        }
    }

    override fun onBindViewLater() {
        super.onBindViewLater()
        viewModel.initVm(intent.extras!!)
    }

    override fun onDestroy() {
        super.onDestroy()
        dataBinding.webView.destroy()
    }
}